package com.ccsu.joysport.controller;

import com.ccsu.joysport.controller.model.resp.AdminClubResp;
import com.ccsu.joysport.dto.*;
import com.ccsu.joysport.entity.Club;
import com.ccsu.joysport.entity.ClubOperator;
import com.ccsu.joysport.repository.ClubOperatorRepository;
import com.ccsu.joysport.service.AdminClubService;
import com.ccsu.joysport.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/ccsu/b/admin/club")
public class AdminClubController {

    @Autowired
    private AdminClubService adminClubService;

    @Autowired
    private ClubOperatorRepository clubOperatorRepository;

    @Autowired
    private JwtUtil jwtUtil;

    /**
     * 俱乐部审核通过
     * 1. update club的俱乐部审核状态为approve
     * 2. add manager添加一条数据，标注club的会长的学号id为root
     *  2.1 如果manager表已经有一条数据，则不用再重复插入
     */

    /**
     * 俱乐部 审核学生的申请
     * 前提：学生申请了俱乐部，club_application已经存在一条数据  application_time  clubId  status(wait) studentId(申请加入俱乐部的学生id) reason
     *
     * 1. update（club_id + student_id） club_application表的数据  将status改为approve/reject
     * 2. 审核通过 - add 将学生信息添加到student_club
     */

    /**
     * 俱乐部 学生的移除
     *   1. remove 将student_club移除学生信息（标准：club_id + student_id 全equals）
     */

    /**
     * 俱乐部审核/拒绝
     */
    @PostMapping("/handleCreateClub")
    public ResponseEntity<?> handleCreateClub(@RequestBody HandleCreateClubRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        HandleCreateClubResponse response = adminClubService.handleCreateClub(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    /**
     *  俱乐部 审核学生的申请
     */
    @PostMapping("/handleJoinClub")
    public ResponseEntity<?> handleJoinClub(@RequestBody HandleJoinClubRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        HandleJoinClubResponse response = adminClubService.handleJoinClub(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }


    @GetMapping("/review/list")
    public ResponseEntity<?> reviewList(@RequestParam String adminToken) {
        if (!jwtUtil.validateToken(adminToken)) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        List<AdminClubResp> list = adminClubService.reviewList();
        return new ResponseEntity<>(list, HttpStatus.OK);
    }


    //新增俱乐部
    @PostMapping("/addClubInfo")
    public ResponseEntity<?> addClubInfo(@RequestBody AddClubInfoRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        ClubResponse response = adminClubService.addClubInfo(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //删除俱乐部
    @DeleteMapping("/deleteClubInfo")
    public ResponseEntity<?> deleteClubInfo(@RequestBody DeleteClubInfoRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        ClubResponse response = adminClubService.deleteClubInfo(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //编辑俱乐部
    @PostMapping("/editClubInfo")
    public ResponseEntity<?> editClubInfo(@RequestBody EditClubInfoRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        ClubResponse response = adminClubService.editClubInfo(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //查看俱乐部
    @GetMapping("/viewClubInfo")
    public ResponseEntity<?> viewClubInfo(@RequestParam String adminToken, @RequestParam String clubId) {
        if (!jwtUtil.validateToken(adminToken)) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        Club club = adminClubService.viewClubInfo(clubId);
        return new ResponseEntity<>(club, HttpStatus.OK);
    }

    //新增操作员
    @PostMapping("/inviteOperator")
    public ResponseEntity<?> inviteOperator(@RequestBody InviteOperatorRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        ClubResponse response = adminClubService.inviteOperator(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //删除操作员
    @PostMapping("/deleteOperator")
    public ResponseEntity<?> deleteOperator(@RequestBody DeleteOperatorRequest request) {
        if (!jwtUtil.validateToken(request.getAdminToken())) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        ClubResponse response = adminClubService.deleteOperator(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    //提供俱乐部
    @GetMapping("/getManagedClubs")
    public ResponseEntity<?> getManagedClubs(@RequestParam String adminToken) {
        String adminId;
        try {
            adminId = jwtUtil.extractId(adminToken);
        } catch (Exception e) {
            return new ResponseEntity<>("token解析失败", HttpStatus.UNAUTHORIZED);
        }

        List<ClubResponse> managedClubs = adminClubService.getManagedClubs(adminId);
        return new ResponseEntity<>(managedClubs, HttpStatus.OK);
    }

    //俱乐部操作者管理
    @GetMapping("/getOperators")
    public ResponseEntity<?> getOperators(@RequestParam String adminToken, @RequestParam String clubId) {
        String adminId;
        try {
            adminId = jwtUtil.extractId(adminToken);
        } catch (Exception e) {
            return new ResponseEntity<>("token解析失败", HttpStatus.UNAUTHORIZED);
        }

        List<OperatorResponse> operatorResponses = adminClubService.getOperators(clubId);
        return new ResponseEntity<>(operatorResponses, HttpStatus.OK);
    }

    //俱乐部已加入成员管理
    @GetMapping("/getMembers")
    public ResponseEntity<?> getMembers(@RequestParam String adminToken, @RequestParam String clubId) {
        String adminId;
        try {
            adminId = jwtUtil.extractId(adminToken);
        } catch (Exception e) {
            return new ResponseEntity<>("token解析失败", HttpStatus.UNAUTHORIZED);
        }

        List<MemberResponse> memberResponses = adminClubService.getMembers(clubId);
        return new ResponseEntity<>(memberResponses, HttpStatus.OK);
    }


    //申请成为俱乐部成员管理
    @GetMapping("/getApplications")
    public ResponseEntity<?> getApplications(@RequestParam String adminToken, @RequestParam String clubId) {
        String adminId;
        try {
            adminId = jwtUtil.extractId(adminToken);
        } catch (Exception e) {
            return new ResponseEntity<>("token解析失败", HttpStatus.UNAUTHORIZED);
        }

        List<ApplicationResponse> applicationResponses = adminClubService.getApplications(clubId);
        return new ResponseEntity<>(applicationResponses, HttpStatus.OK);
    }

    //删除俱乐部已有的成员
    @DeleteMapping("/removeMember")
    public ResponseEntity<DeleteClubResponse> removeMember(@RequestBody DeleteMemberRequest request) {
        String adminId;
        try {
            adminId = jwtUtil.extractId(request.getAdminToken());
        } catch (Exception e) {
            return new ResponseEntity<>(new DeleteClubResponse("failure", "token解析失败"), HttpStatus.UNAUTHORIZED);
        }

        DeleteClubResponse response = adminClubService.deleteMember(request);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }
    //返回所有俱乐部名称和id
    @GetMapping("/getAllClub")
    public ResponseEntity<?> getAllClubs(@RequestParam String adminToken) {
        if (!jwtUtil.validateToken(adminToken)) {
            return new ResponseEntity<>("无效的管理员token", HttpStatus.UNAUTHORIZED);
        }

        List<AllClubResponse> clubs = adminClubService.getAllClubs();
        return ResponseEntity.ok(clubs);
    }
}
